//
//  BaseView.m
//  BoWuGuan
//
//  Created by 梁尚嘉 on 2016/10/31.
//  Copyright © 2016年 KT. All rights reserved.
//

#import "BaseView.h"

@implementation BaseView

- (instancetype)init
{
    if (self = [super init]) {
        self.opaque = YES;
    }
    return self;
}

- (void)didSelectBasketButton:(UIButton *)button
{

    [self didClickPlusButton:button];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        KTSystemStorageSingleton.numOfCartItem ++;
    });
}



- (void)didClickPlusButton:(UIButton *)button
{
    CGPoint generatePoint = [button.superview convertPoint:button.center toView:SMKeyWindow];
    CGPoint middlePoint = CGPointMake(generatePoint.x-SM_SIZE(200), generatePoint.y-SM_SIZE(200));
    CGPoint distinatePoint = [KTSystemStorageSingleton.badgeView.superview convertPoint:KTSystemStorageSingleton.badgeView.center toView:SMKeyWindow];;

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:generatePoint];
    [path addQuadCurveToPoint:distinatePoint controlPoint:middlePoint];

    CAShapeLayer *dot   = [CAShapeLayer layer];
    CGFloat width       = SM_SIZE(30);
    dot.frame           = CGRectMake(0, 0, width, width);
    dot.position        = distinatePoint;
    dot.backgroundColor = SM_dotRedColor.CGColor;
    dot.cornerRadius    = width/2;
    dot.masksToBounds   = YES;
    [SMKeyWindow.layer addSublayer:dot];

    double duration                 = .5f;
    CAKeyframeAnimation *animation  = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path                  = path.CGPath;
    animation.duration              = duration;
    animation.timingFunction        = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [dot addAnimation:animation forKey:nil];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [dot removeFromSuperlayer];
        [KTSystemStorageSingleton.badgeView addInstanceScaleAnimationWithTargetScale:1.1
                                                                             reverse:YES
                                                                            duration:.1];
    });
    
}
@end
